using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Microsoft.PowerShell.CmdletManagement;

namespace Microsoft.PowerShell.CmdletManagement
{
    /// <summary>
    /// Interaction logic for AddCmdlet.xaml
    /// </summary>

    public partial class AddCmdlet : System.Windows.Window
    {

        public AddCmdlet(Window owner)
        {
            InitializeComponent();

            this.Owner = owner;
            this.Verb.Items.Focus();
        }

        public string VerbName
        {
            get 
            {
                PsSpecVerb verb = (PsSpecVerb)this.Verb.Items.SelectedItem;
                
                if (verb == null)
                    return null;
                else
                    return (string) verb.Name; 
            }
        }

        public string NounName
        {
            get 
            {
                PsSpecNoun noun = (PsSpecNoun)this.Noun.Items.SelectedItem;

                if (noun == null)
                    return null;
                else
                    return (string)noun.Name; 
            }
        }

        // They've clicked "OK"
        private void Click_Ok(Object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }
        // They've clicked "Cancel"
        private void Click_Cancel(Object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        // Refresh the view when data is loaded
        private void Cmdlet_Loaded(Object sender, EventArgs e)
        {
            ICollectionView collectionView = CmdletCollectionView;
            if (collectionView != null) { collectionView.Refresh(); }
        }

        // They've changed the search text
        private void Search_TextChanged(Object sender, EventArgs e)
        {
            if (CmdletList != null)
            {
                ICollectionView collectionView = CmdletCollectionView;
                if (collectionView != null)
                {
                    collectionView.Refresh();
                }
            }
        }

        // The collection view that supports the cmdlet filtering and sorting
        private ICollectionView CmdletCollectionView
        {
            get
            {
                if (cmdletCollectionView == null)
                {
                    lock (lockObject)
                    {
                        if (cmdletCollectionView == null)
                        {
                            cmdletCollectionView = CollectionViewSource.GetDefaultView(CmdletList.DataContext);

                            if (cmdletCollectionView != null)
                            {
                                cmdletCollectionView.Filter = new Predicate<object>(CmdletTextFilter);

                                cmdletCollectionView.SortDescriptions.Clear();
                                SortDescription sd = new SortDescription("Verb", ListSortDirection.Ascending);
                                cmdletCollectionView.SortDescriptions.Add(sd);
                                cmdletCollectionView.Refresh();
                            }
                        }
                    }
                }

                return cmdletCollectionView;
            }
        }
        ICollectionView cmdletCollectionView = null;
        private object lockObject = new object();

        // The delegate that determines when to display a cmdlet
        protected bool CmdletTextFilter(object item)
        {
            PsSpecCmdlet cmdlet = item as PsSpecCmdlet;
            if (cmdlet == null)
                return true;

            return CmdletDesignerUtils.DoesSearchMatchCmdlet(cmdlet, CmdletSearch.Text);
        }

        // They focused on the search box
        private void Search_GotFocus(Object sender, EventArgs e)
        {
            CmdletSearch.Foreground = Brushes.Black;

            if (CmdletSearch.Text == "Search")
            {
                CmdletSearch.Text = "";
            }
        }

        private void Search_LostFocus(Object sender, EventArgs e)
        {
            CmdletSearch.Foreground = new SolidColorBrush(Color.FromArgb(128, 0, 0, 0));

            if (String.IsNullOrEmpty(CmdletSearch.Text) || (CmdletSearch.Text == "Search"))
            {
                CmdletSearch.Text = "Search";
            }
        }
   }
}